package com.changeover.gwt.client.view.impl;

import com.changeover.gwt.client.view.ICustomerDetails;
import com.changeover.gwt.shared.model.customer.ICustomer;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.LayoutPanel;
import com.google.gwt.user.client.ui.TabPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.CaptionPanel;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.datepicker.client.DateBox;
import com.google.gwt.user.client.ui.TextArea;

public class CustomerDetailsView extends Composite implements ICustomerDetails {

    private ICustomer currentCustomer;
    private Grid customerDataGrid;
    ListBox statusCombo;

    public CustomerDetailsView() {
        
        VerticalPanel verticalPanel = new VerticalPanel();
        initWidget(verticalPanel);
        verticalPanel.setSize("680px", "269px");
        
        Button btnClose = new Button("Close");
        btnClose.setText("Abandon");
        btnClose.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                ((DialogBox)getParent().getParent()).hide();
            }
        });
        
        TabPanel tabPanel = new TabPanel();
        verticalPanel.add(tabPanel);
        
        VerticalPanel customerAccountPanel = new VerticalPanel();
        tabPanel.add(customerAccountPanel, "Customer/Account");
        customerAccountPanel.setSize("675px", "185px");
        Label customerDataLabel = new Label("Customer data");
        
        VerticalPanel customerDataWrapper = new VerticalPanel();
        customerAccountPanel.add(customerDataWrapper);
        customerDataWrapper.setSize("675px", "76px");
        
        Label customerDataLabel_1 = new Label("Customer data");
        customerDataWrapper.add(customerDataLabel_1);
        
        customerDataGrid = new Grid(2, 7);
        customerDataWrapper.add(customerDataGrid);
        customerDataGrid.setWidth("674px");
        customerDataGrid.getRowFormatter().getElement(0).getStyle().setBackgroundColor("#BBBBBB");
        
        customerDataGrid.setStyleName((String) null);
        customerDataGrid.setBorderWidth(1);
        customerDataGrid.setText(0, 0, "ID");
        customerDataGrid.setText(0, 1, "Name");
        customerDataGrid.setText(0, 2, "Address");
        customerDataGrid.setText(0, 3, "ZIP");
        customerDataGrid.setText(0, 4, "City");
        customerDataGrid.setText(0, 5, "Telephone");
        customerDataGrid.setText(0, 6, "Mobile");
        
        LayoutPanel layoutPanel = new LayoutPanel();
        customerAccountPanel.add(layoutPanel);
        layoutPanel.setSize("", "84px");
        
        Label lblCompanyMembers = new Label("Company members");
        layoutPanel.add(lblCompanyMembers);
        layoutPanel.setWidgetLeftWidth(lblCompanyMembers, 0.0, Unit.PX, 130.0, Unit.PX);
        layoutPanel.setWidgetTopHeight(lblCompanyMembers, 0.0, Unit.PX, 18.0, Unit.PX);
        
        Grid companyMembersGrid = new Grid(2, 2);
        companyMembersGrid.setBorderWidth(1);
        layoutPanel.add(companyMembersGrid);
        layoutPanel.setWidgetRightWidth(companyMembersGrid, 525.0, Unit.PX, 150.0, Unit.PX);
        companyMembersGrid.setSize("150px", "50px");
        layoutPanel.setWidgetTopHeight(companyMembersGrid, 24.0, Unit.PX, 100.0, Unit.PX);
        companyMembersGrid.getRowFormatter().getElement(0).getStyle().setBackgroundColor("#BBBBBB");
        companyMembersGrid.setText(0, 0, "ID");
        companyMembersGrid.setText(0, 1, "Name");
        
        Grid ownAccountsGrid = new Grid(2, 3);
        ownAccountsGrid.setBorderWidth(1);
        layoutPanel.add(ownAccountsGrid);
        layoutPanel.setWidgetLeftWidth(ownAccountsGrid, 185.0, Unit.PX, 230.0, Unit.PX);
        layoutPanel.setWidgetTopHeight(ownAccountsGrid, 24.0, Unit.PX, 52.0, Unit.PX);
        ownAccountsGrid.setWidth("230px");
        ownAccountsGrid.getRowFormatter().getElement(0).getStyle().setBackgroundColor("#BBBBBB");
        ownAccountsGrid.setText(0, 0, "ID");
        ownAccountsGrid.setText(0, 1, "Type");
        ownAccountsGrid.setText(0, 2, "Authorized");
        
        Grid authorizedForGrid = new Grid(2, 3);
        authorizedForGrid.setBorderWidth(1);
        layoutPanel.add(authorizedForGrid);
        layoutPanel.setWidgetLeftWidth(authorizedForGrid, 445.0, Unit.PX, 230.0, Unit.PX);
        authorizedForGrid.setWidth("230px");
        layoutPanel.setWidgetTopHeight(authorizedForGrid, 24.0, Unit.PX, 100.0, Unit.PX);
        authorizedForGrid.getRowFormatter().getElement(0).getStyle().setBackgroundColor("#BBBBBB");
        authorizedForGrid.setText(0, 0, "ID");
        authorizedForGrid.setText(0, 1, "Type");
        authorizedForGrid.setText(0, 2, "Owner");
        
        Label lblNewLabel = new Label("Own accounts");
        layoutPanel.add(lblNewLabel);
        layoutPanel.setWidgetLeftWidth(lblNewLabel, 185.0, Unit.PX, 150.0, Unit.PX);
        layoutPanel.setWidgetTopHeight(lblNewLabel, 0.0, Unit.PX, 18.0, Unit.PX);
        
        Label lblAuthorizedFor = new Label("Authorized for");
        layoutPanel.add(lblAuthorizedFor);
        layoutPanel.setWidgetLeftWidth(lblAuthorizedFor, 445.0, Unit.PX, 100.0, Unit.PX);
        layoutPanel.setWidgetTopHeight(lblAuthorizedFor, 0.0, Unit.PX, 18.0, Unit.PX);
        
        
        VerticalPanel statusPanel = new VerticalPanel();
        tabPanel.add(statusPanel, "Status");
        statusPanel.setSize("680px", "189px");
        
        CaptionPanel actionWrapper = new CaptionPanel("Action");
        statusPanel.add(actionWrapper);
        actionWrapper.setWidth("480px");
        
        LayoutPanel actionLayoutPanel = new LayoutPanel();
        actionWrapper.setContentWidget(actionLayoutPanel);
        actionLayoutPanel.setSize("482px", "126px");
        
        Label lblStatus = new Label("Status");
        actionLayoutPanel.add(lblStatus);
        actionLayoutPanel.setWidgetLeftWidth(lblStatus, 0.0, Unit.PX, 65.0, Unit.PX);
        actionLayoutPanel.setWidgetTopHeight(lblStatus, 10.0, Unit.PX, 18.0, Unit.PX);
        
        statusCombo = new ListBox();
        statusCombo.addItem("correspondence started", "1");
        statusCombo.addItem("not responding", "2");
        actionLayoutPanel.add(statusCombo);
        actionLayoutPanel.setWidgetLeftWidth(statusCombo, 71.0, Unit.PX, 129.0, Unit.PX);
        actionLayoutPanel.setWidgetTopHeight(statusCombo, 10.0, Unit.PX, 22.0, Unit.PX);
        
        Label lblSetReminder = new Label("Set reminder");
        actionLayoutPanel.add(lblSetReminder);
        actionLayoutPanel.setWidgetLeftWidth(lblSetReminder, 224.0, Unit.PX, 91.0, Unit.PX);
        actionLayoutPanel.setWidgetTopHeight(lblSetReminder, 10.0, Unit.PX, 18.0, Unit.PX);
        
        DateBox remainderDateBox = new DateBox();
        actionLayoutPanel.add(remainderDateBox);
        actionLayoutPanel.setWidgetLeftWidth(remainderDateBox, 321.0, Unit.PX, 129.0, Unit.PX);
        actionLayoutPanel.setWidgetTopHeight(remainderDateBox, 10.0, Unit.PX, 23.0, Unit.PX);
        
        Label lblComment = new Label("Comment");
        actionLayoutPanel.add(lblComment);
        actionLayoutPanel.setWidgetLeftWidth(lblComment, 0.0, Unit.PX, 65.0, Unit.PX);
        actionLayoutPanel.setWidgetTopHeight(lblComment, 45.0, Unit.PX, 22.0, Unit.PX);
        
        TextArea commentTextArea = new TextArea();
        actionLayoutPanel.add(commentTextArea);
        actionLayoutPanel.setWidgetLeftWidth(commentTextArea, 71.0, Unit.PX, 379.0, Unit.PX);
        actionLayoutPanel.setWidgetTopHeight(commentTextArea, 45.0, Unit.PX, 53.0, Unit.PX);
        
        Button actionSaveButton = new Button("Save");
        actionLayoutPanel.add(actionSaveButton);
        actionLayoutPanel.setWidgetLeftWidth(actionSaveButton, 400.0, Unit.PX, 82.0, Unit.PX);
        actionLayoutPanel.setWidgetTopHeight(actionSaveButton, 104.0, Unit.PX, 22.0, Unit.PX);
        
        CaptionPanel historyWrapper = new CaptionPanel("History");
        statusPanel.add(historyWrapper);
        historyWrapper.setWidth("480px");
        tabPanel.selectTab(0);
        
        verticalPanel.add(btnClose);
        verticalPanel.setCellHorizontalAlignment(btnClose, HasHorizontalAlignment.ALIGN_RIGHT);
        btnClose.setWidth("75px");
    }
    
    public Widget asWidget() {
        return this;
    }
    
    public void  setCustomer(ICustomer customer) {
        currentCustomer = customer;
        customerDataGrid.setText(1, 0, customer.getId().toString());
        customerDataGrid.setText(1, 1, customer.getName());
        statusCombo.setItemSelected(customer.getLastAction().getId() - 1, true);
    }
}
